package com.mobile.framework.util;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2009-2-17
 * Time: 12:01:05
 * To change this template use File | Settings | File Templates.
 */
public class ByteUtil {
    public static List insertByte(List list, byte[] y, int start, int end) {
        for (int i = start; i <= end; i++) {
            list.add(y[i - start]);
        }
        return list;
    }

    public static byte[] converLongToBytes(long l) {
        byte[] b = new byte[8];
        b = java.lang.Long.toString(l).getBytes();
        return b;
    }

    public static byte[] converIntToBytes(int l) {
        byte[] b = new byte[4];
        b = java.lang.Integer.toString(l).getBytes();
        return b;
    }

    //int转换成长度为4的byte数组,长度为4的byte数组合成一个int.
    public static int bytes2int(byte[] b) {
        //byte[] b=new byte[]{1,2,3,4};
        int mask = 0xff;
        int temp = 0;
        int res = 0;
        for (int i = 0; i < 4; i++) {
            res <<= 8;
            temp = b[i] & mask;
            res |= temp;
        }
        return res;
    }

    public static byte[] int2bytes(int num) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            b[i] = (byte) (num >>> (24 - i * 8));
        }
        return b;
    }

    //long转换成长度为8的byte数组,长度为8的byte数组合成一个long
    public static int bytes2long(byte[] b) {

        int mask = 0xff;
        int temp = 0;
        int res = 0;
        for (int i = 0; i < 8; i++) {
            res <<= 8;
            temp = b[i] & mask;
            res |= temp;
        }
        return res;
    }

    public static byte[] long2bytes(long num) {
        byte[] b = new byte[8];
        for (int i = 0; i < 8; i++) {
            b[i] = (byte) (num >>> (56 - i * 8));
        }
        return b;
    }


}
